package com.icloud.system.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icloud.common.core.api.ResultCode;
import com.icloud.common.web.enums.FileTypeEnum;
import com.icloud.common.web.exception.ApiException;
import com.icloud.common.web.util.S3Util;
import com.icloud.common.web.vo.FileUrlVO;
import com.icloud.common.web.vo.PageVO;
import com.icloud.system.boot.dto.InformationPageDto;
import com.icloud.system.boot.dto.UpdateAppCustomerDto;
import com.icloud.system.boot.mapper.TInformationMapper;
import com.icloud.system.boot.model.TInformation;
import com.icloud.system.boot.service.ITInformationService;
import com.icloud.system.boot.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.services.s3.S3Client;

import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TInformationServiceImpl extends ServiceImpl<TInformationMapper, TInformation> implements ITInformationService {
    @Autowired
    private S3Client s3Client;

    @Override
    public FileUrlVO uploadFileCover(MultipartFile file) {
        long size = file.getSize();
        if(size>1024*1024*40){
            throw new ApiException(ResultCode.FILE_UPLOAD_IMG_TOO_LARGE);
        }
        return S3Util.uploadFile(s3Client, file, FileTypeEnum.COVER.getCode());
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean addInfomation(AddInformationVO vo) {
        TInformation tInformation = new TInformation();
        BeanUtils.copyProperties(vo, tInformation);
        String title = vo.getTitle();
        String coverUrl = vo.getCoverUrl();
        if(StringUtils.isBlank(title)){
            throw new ApiException(ResultCode.REQUIRED_TITLE);
        }
        if(StringUtils.isBlank(coverUrl)){
            throw new ApiException(ResultCode.REQUIRED_IMG_URL);
        }
        int effect = baseMapper.insert(tInformation);
        return effect>0;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean updateInfomation(UpdateInformationVO vo) {
        TInformation tInformation = new TInformation();
        BeanUtils.copyProperties(vo, tInformation);
        String title = vo.getTitle();
        String coverUrl = vo.getCoverUrl();
        Long id = vo.getId();
        if(id==null || id<=0){
            throw new ApiException(ResultCode.REQUIRED_INFOMATION_ID);
        }
        if(StringUtils.isBlank(title)){
            throw new ApiException(ResultCode.REQUIRED_TITLE);
        }
        if(StringUtils.isBlank(coverUrl)){
            throw new ApiException(ResultCode.REQUIRED_IMG_URL);
        }
        baseMapper.updateById(tInformation);
        return true;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean deleteInfomation(DeleteInformationVO vo) {
        Long id = vo.getId();
        if(id==null || id<=0){
            throw new ApiException(ResultCode.REQUIRED_INFOMATION_ID);
        }
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public PageVO<InformationVO> infomationPage(InformationPageDto dto) {
        Long current = dto.getCurrent();
        if(current==null){
            current=0L;
        }
        Long pageSize = dto.getPageSize();
        if(pageSize==null){
            pageSize=0L;
        }
        Page<TInformation> page = new Page<>();
        page.setCurrent(current);
        page.setSize(pageSize);
        List<TInformation> infoList = null;
        LambdaQueryWrapper<TInformation> infoQuery = new LambdaQueryWrapper<>();
        infoQuery.orderByDesc(TInformation::getUpdatedAt);
        if(current==0 || pageSize==0){
            infoList = baseMapper.selectList(infoQuery);
        }else {
            Page<TInformation> infoPage = baseMapper.selectPage(page, infoQuery);
            infoList = infoPage.getRecords();
        }
        PageVO<InformationVO> pageVO = new PageVO<>();
        pageVO.setTotal(0L);
        if(!CollectionUtils.isEmpty(infoList)){
            pageVO.setTotal((long) infoList.size());

            List<InformationVO> voList = infoList.stream().map(e->{
                InformationVO infoVo = new InformationVO();
                BeanUtils.copyProperties(e, infoVo);
                infoVo.setCreatedAt(e.getCreatedAt().toEpochSecond(ZoneOffset.of("+8")));
                return infoVo;
            }).collect(Collectors.toList());
            pageVO.setList(voList);
            if(page.getTotal()>0) {
                pageVO.setTotal(page.getTotal());
            }
        }
        return pageVO;
    }
}
